<?php

class Etudiant {
	private $enseignants;
	private $nom;
	
	public function __construct($nom, $enss=null) {
		$this->nom=$nom;
		
		$this->enseignants=array();
		
		if (isset($enss)) {
			if ( is_array($enss) ) {
				$this->enseignants=array_unique($enss);
			} else {
				array_push($this->enseignants, $enss);
			}
		}
		
		foreach ($this->enseignants as $ens) { $ens->addEtudiant($this); }
	}
	
	public function __destruct() {	
		foreach ($this->enseignants as $ens) { $ens->delEtudiant($this); }
	}
	
	
	public function __toString() {
		$str="Etudiant [ Nom: ".$this->nom."]";
		if (count($this->enseignants) > 0) {
			$str.="\n\tsuit les cours de :";
			foreach ($this->enseignants as $ens) { $str.="\n\t\t * ". $ens->getNom(); }
		} else { $str.="\n\tPas d enseignant."; }
		$str.="\n";
		return $str;
	}
	
	public function addEnseignant($ens, $propage=true) {
		if (!in_array($ens, $this->enseignants)) array_push($this->enseignants, $ens);
		if ($propage) $ens->addEtudiant($this, false);
	}
	
	public function delEnseignant($ens, $propage=true) {
		$this->enseignants= array_diff($this->enseignants, array($ens));
		if ($propage) $ens->delEtudiant($this, false);
	}
	public function getNom() { return $this->nom; }
}
?>